home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PsL Monthly 1993 December
/
PSL Monthly Shareware CD-ROM (December 1993).iso
/
prgmming
/
dos
/
c
/
str.exe
/
STR.H
< prev
next >
Wrap
Text File
|
1993-03-01
|
10KB
|
319 lines
//
// str.h : str class interface
// Author : Roy S. Woll
//
// Copyright (c) 1993 by Roy S. Woll
// You may distribute this source freely as long as you leave all files
// in their original form, including the copyright notice as is.
//
// Version 2.01 3/1/93 Support Turbo C++ (no nested class)
// Version 2.00 12/1/92
//
#ifndef _STR_H
#define _STR_H
//#define DEBUG_STR
class ostream;
class istream;
class dynstream;
class regX;
#ifdef __TURBOC__
#ifndef __BORLANDC__
#define _SUBSTR substr
#else
#define _SUBSTR str::substr
class str {
#endif
#else
#define _SUBSTR str::substr
class str {
#endif
class substr{
friend class str;
int posReplace;
int numReplace;
str * mystr;
substr(const str * data, int AposReplace, int AnumReplace);
int compare(const char *) const;
int compare(const substr&) const;
int length() const;
public:
str & operator = (const substr& s);
str & operator = (const char * s);
str operator+(const char *) const;
str operator+(const substr&) const;
int operator==(const char *) const;
int operator<=(const char *) const;
int operator>=(const char *) const;
int operator!=(const char *) const;
int operator< (const char *) const;
int operator> (const char *) const;
int operator==(const substr&) const;
int operator<=(const substr&) const;
int operator>=(const substr&) const;
int operator!=(const substr&) const;
int operator< (const substr&) const;
int operator> (const substr&) const;
operator str() const;
};
#ifdef __TURBOC__
#ifndef __BORLANDC__
class str {
#endif
#endif
friend class dynstreambuf;
protected:
enum {STR_DEFAULT_MEMINCR=256};
enum {ICASE=1};
#ifdef DEBUG_STR
public:
static int dynstreamCount;
static int ObjectCount;
static int AllocationCount;
static int TotalObjectCount;
static int TotalAllocationCount;
#endif
friend class substr;
struct strdata{
enum {STR_DEBUG_BUFSIZE=80};
int cursize; // cursize should be defined before buf!
int curlength; // current length of buffer
int refcount;
dynstream * mystream;// pointer to str's stream
char strChange; // Internal flag indicating when the length has been
// changed by the str class.
// If true, stream is notified.
char buf[STR_DEBUG_BUFSIZE];
// buf[1] is all that is required, but we allow
// it to be buf[80] so that the debugger can
// treat it as a char *.
};
static int defaultFlags;
int memsize_init; // Initial amount of memory to allocate for buffer
int memsize_incr; // Memory expansion increment
int flags; // Default flags for case sensitivity, etc.
str& _assign (const char * source, int len);
str& _concat (const char * source, int len);
int _checkMemAllocation(int requiredLen=0);
static void init(strdata *&, int, int);
protected:
strdata * data;
char * getNewBuffer( int len, int newbufsize);
char * getNewBuffer(int newbufsize);
void setNewBuffer(strdata * newdata, int newbufsize, int len);
void setlength(int len) const; // update the current length
int strncmp(const char * s1, const char * s2, int n) const;
int strcmp(const char * s1, const char * s2) const;
//
// Special constructors used by implementation
//
str(const char *, const char *);
str(const char *, const _SUBSTR&);
str(const substr&, const char *);
str(const substr&, const substr&);
public:
//
// Constructors
//
str(void);
str(int p_bufsize, int = STR_DEFAULT_MEMINCR);
str(const char * s, int bufsize=0, int = STR_DEFAULT_MEMINCR);
str(const str&, int bufsize=0, int = STR_DEFAULT_MEMINCR);
virtual ~str();
//
// Access Operators
//
int size(void) const; // return current memory allocated for buffer
int length(void) const; // return current string length of buffer
operator const char * () const; // for implicit casting
const char * operator()() const; // for explicit casting
const char * operator()(int index) const; //
//
// String substitution using substr "() syntax"
//
substr operator()(int pos, int numreplace); // create substr from str
const substr operator()(int pos, int numreplace) const;
//
// String search/replace member functions
//
int search (const char *, int * startPtr) const;
int search (const char *, int start=0) const;
int search (const regX&, int * startPtr) const;
int search (const regX&, int start) const;
int search (const regX&, str * matchPtr=0, int start=0) const;
int search (const regX&, str * matchPtr, int * startPtr) const;
int index (const char *, int start=0) const;
int index (const regX&, int start=0) const;
int index (const regX&, int * matchLen, int start=0) const;
int replace (const regX&, const char * replaceString,
int* startPtr, int numReplacements=1);
int replace (const char * pattern, const char * replaceString,
int* startPtr, int numReplacements=1);
int replace (const regX&, const char * replaceString,
int start=0, int numReplacements=1);
int replace (const char * pattern, const char * replaceString,
int start=0, int numReplacements=1);
int replaceAll (const char *, const char * replaceString, int start=0);
int replaceAll (const regX&, const char * replaceString, int start=0);
char& operator[](int position); // array indexing
char operator[](int position) const; // array indexing for const
ostream& stream(void); // return stream for this str
ostream& stream(int); // return stream and move put pointer
//
// Assignment & Concatenation Operators
//
str & operator = (const str & s); // s = str;
str & operator = (const substr & s); // s = substr;
str & operator = (const char * s); // s = charptr
str & operator = (const char s); // s = character
str & assign (const char * source, int len=0);
str & operator += (const str & s); // s += str
str & operator += (const substr & s); // s += substr
str & operator += (const char * s); // s += charptr
str & operator += (const char s); // s += char
str & operator << (const str& s); // s << str
str & operator << (const substr& s); // s << substr
str & operator << (const char * s); // s << charptr
str & operator << (const int s); // s << int
str & operator << (const char s); // s << char
// Manipulators
enum PadStripT {left, right, both, leading=0, trailing};
str& strip(PadStripT t=trailing, const char * stripchar=" \t");
str& strip(PadStripT t, char stripchar);
str& pad(int padsize, PadStripT t=right, char padchar = ' ');
// Mutators
int insert(int pos, char ch); // insert ch, return 0 if fail
int insert(int pos, const char * insertStr);
// insert char *, return 0 if fail
void remove(int pos, int numdel=1); // remove numdel characters starting at
// position pos
str operator+(const str &) const;
str operator+(const substr &) const;
str operator+(const char * b) const;
str operator+(const char b) const;
int operator==(const char *) const; // made member functions so that
int operator<=(const char *) const; // derived classes inherit functions
int operator>=(const char *) const;
int operator!=(const char *) const;
int operator< (const char *) const;
int operator> (const char *) const;
int operator==(const str &) const;
int operator<=(const str &) const;
int operator>=(const str &) const;
int operator!=(const str &) const;
int operator